﻿package de.gameduell.framework.util {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;		

	/**
	 * @author GD
	 */
	public class Library {
		
		private static var instance:Library = null;
		
		private var _graphicsDomain:ApplicationDomain;
		private var _soundsDomain:ApplicationDomain;

		public static function getInstance():Library{
			if(instance == null){
				instance = new Library();
			}
			return instance;
		}
		
		/**
		 * constructor
		 * 
		 * ATTENTION:
		 * do not call this it's a singelton
		 */
		public function Library() {
			_graphicsDomain = null;
			_soundsDomain = null;;		
		}
		
		public function set soundsDomain(aDomain:ApplicationDomain):void {
			_soundsDomain = aDomain;
		}
		
		public function set graphicsDomain(aDomain:ApplicationDomain):void{
			_graphicsDomain = aDomain;
		}
		
		public function get graphicsDomain():ApplicationDomain{
			return _graphicsDomain;
		}
		
		public function get soundsDomain():ApplicationDomain{
			return _soundsDomain;
		}
		
		/**
		 * returns a class form the library
		 * 
		 * @param className the name of the class you want
		 */
		public function getClass(className:String, appDomain:ApplicationDomain = null):Class {
			if(appDomain == null) {
				return Class(getDefinitionByName(className));                    
			}else{
				return Class(appDomain.getDefinition(className));
			}                
		}

		/**
		  * creates an instance of a library class
		  * 
		  * @param className the name of the class from wich you want the instance
		  */
		 public function createInstanceOf(className:String,appDomain:ApplicationDomain = null):Object{
		 	var libraryClass:Class = getClass(className, appDomain) as Class;
			return new libraryClass();	 	
		 }
		 
		 /**
		  * creates a MovieClip instance from the library
		  * 
		  * @param className the name of the MovieClip you want
		  */
		 public function createClip(className:String, appDomain:ApplicationDomain = null):MovieClip{
			var domain:ApplicationDomain = appDomain == null ? _graphicsDomain : appDomain;
		 	return MovieClip(createInstanceOf(className, domain));
		 }
		 
		 /**
		  * creates a Bitmap instance from the library
		  * 
		  * @param className the name of the Bitmap you want
		  */
		 public function createBitmap(className:String, width:uint = 1, height:uint = 1, appDomain:ApplicationDomain = null):Bitmap{
			var domain:ApplicationDomain = appDomain == null ? _graphicsDomain : appDomain;
		 	var bitmapDataClass:Class = getClass(className, domain) as Class;
		 	var res:Bitmap = new Bitmap();
		 	res.bitmapData = new bitmapDataClass(width, height);
		 	return res;
		 }
				 
		public function createBitmapData(className:String, width:int, height:int, appDomain:ApplicationDomain = null):BitmapData {
			var domain:ApplicationDomain = appDomain == null ? _graphicsDomain : appDomain;
			var libraryClass:Class = getClass(className, domain) as Class;
			return new libraryClass(width,height);             
		}
		 
		 public function createSprite(className:String,appDomain:ApplicationDomain = null):Sprite{
			var domain:ApplicationDomain = appDomain == null ? _graphicsDomain : appDomain;
		 	return Sprite(createInstanceOf(className, domain));
		 }
		 
		 public function createDisplayObjectContainer(className:String,appDomain:ApplicationDomain = null):DisplayObjectContainer{
		 	var domain:ApplicationDomain = appDomain == null ? _graphicsDomain : appDomain;
			return DisplayObjectContainer(createInstanceOf(className,domain));
		 }
		 
		 /**
		  * creates aSound instance from the library
		  * 
		  * @param className the name of the Sound you want
		  */ 
		 public function createSound(className:String,appDomain:ApplicationDomain = null):Sound{
			var domain:ApplicationDomain = appDomain == null ? _soundsDomain : appDomain;
		 	return Sound(createInstanceOf(className, domain));
		 }
	}
}
